<?php
namespace app\tools;
use app\exception\CustomException;

class Curl
{
    private static $deviceId = null;
    private static $cookie = null;

    public function __construct($deviceId = null, $cookie= null) {
        self::$deviceId = $deviceId;
        self::$cookie = $cookie;
    }


    public static function curl($url = '', $post_data = [], $cookie = ''){

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if($cookie) curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        if($post_data){
            // post数据
            curl_setopt($ch, CURLOPT_POST, 1);

            // post的变量
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }
        $output = curl_exec($ch);
        curl_close($ch);
        //返回获得的数据
        return $output;
    }

    public static function get_request_payload($url, $param)
    {
        if (!empty($param))
            $url = $url.http_build_query($param);
        $data = null;
        //try {
            $ch = curl_init($url);
            // 必须要来路域名
            //curl_setopt($ch, CURLOPT_REFERER, "https://wx2.qq.com/?&lang=zh_CN");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_COOKIEJAR, self::$cookie);
            $data = curl_exec($ch);
            curl_close($ch);
            if(false === $data)
            {
                throw new CustomException("CURL_ERROR");
            }
//        } catch(\Exception $e) {
//            throw new CustomException($e->getMessage());
//        }
        return $data;
    }

    public static function post_request_payload($url, $post)
    {
        //try {
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36');
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_NOBODY, FALSE);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            $data = curl_exec($ch);

            $body = '';

            if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
                $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
                $body = substr($data, $headerSize);
            }
            curl_close($ch);

            if(false === $data)
            {
                throw new CustomException("CURL_ERROR");
            }

            return $body;
//        } catch(\Exception $e) {
//            throw new CustomException($e->getMessage());
//        }
    }
}


?>